import {Component, OnInit} from '@angular/core';
import {NzModalService} from "ng-zorro-antd/modal";
import {HttpClient} from "@angular/common/http";
import {PermissionCard} from "@/app/pages/disk/file-list/permission-card/permission-card";
import {FileInfo} from "@shared/models/FileInfo";
import {UserSettings} from "@shared/models/UserSettings";
import {UserActionService} from "@core/service/UserActionService";
import {Result} from "@shared/models/Result";
import {Permission} from "@shared/models/Permission";
import {BBYUtils} from "@shared/utils/BBYUtils";
import {NzMessageService} from "ng-zorro-antd/message";
import {DiskService} from "@core/service/DiskService";

@Component({
  selector: 'app-setting',
  templateUrl: './setting.component.html',
  styleUrls: ['./setting.component.scss']
})
export class SettingComponent implements OnInit {
  settings:UserSettings;
  rootFile:FileInfo|undefined;
  loading=true;
  error: { code:string,msg:string }|undefined;

  constructor(
    private modal: NzModalService,
    private http:HttpClient,
    private userActionService:UserActionService,
    private diskService:DiskService,
    private message: NzMessageService
  ) {
    let str = localStorage.getItem("settings")
    if (str){
      this.settings=JSON.parse(str)
    }else {
      this.settings={
        userId: '',
        blockSearch: false,
        defaultPermission: new Permission(),
        defaultPmType:'extends',
      }
    }
    BBYUtils.handelResult(
      diskService.fileInfo(),
      res => {
        if (!res.data.permission){
          res.data.permission=new Permission()
        }
        this.rootFile = res.data
      },
      res => {
        message.error('根目录权限加载失败！')
        console.log(res)
        this.rootFile = undefined;
      }
    )
    BBYUtils.handelResult(
      this.userActionService.getSettings(),
      (res:Result)=>{
        this.settings=res.data;
        this.error=undefined;
        this.loading=false;
        localStorage.setItem("settings",JSON.stringify(this.settings))
      },
      (res)=>{
        this.loading=false;
        this.error={
          code:res.code+'',
          msg:res.msg
        }
      }
    )
  }

  ngOnInit(): void {
  }

  test() {
    this.http.get("http://localhost:8003/disk/search?target=aa").subscribe(console.log)
  }

  saveSettings(){
    this.loading=true
    BBYUtils.handelResult(
      this.userActionService.saveSettings(this.settings),
      (res:Result)=>{
        this.settings=res.data;
        this.message.success('修改成功')
        this.loading=false;
        localStorage.setItem("settings",JSON.stringify(this.settings))
      },
      (res)=>{
        this.loading=false;
        this.message.error(res.msg)
      }
    )
    // BBYUtils.handelResult(
    //   this.userActionService.saveSettings(this.settings)
    // )
  }
  editDefaultPmClick() {
    if (!this.settings.defaultPermission){
      this.settings.defaultPermission=new Permission()
    }
    let file = new FileInfo()
    file.permission=this.settings.defaultPermission
    this.modal.create({
      nzTitle: '文件默认权限',
      nzWidth:"700px",
      nzContent:PermissionCard,
      nzComponentParams:{
        file:file
      },
      nzClosable: false,
      nzOnOk: (page) => new Promise(resolve =>{
        this.settings.defaultPermission=page.permission
        console.log(this.settings)
        this.saveSettings()
        resolve()
      })
    });
  }

  editRootPmClick() {
    if (!this.rootFile || !this.rootFile.permission){
      return;
    }
    this.modal.create({
      nzTitle: '根目录权限',
      nzWidth:"700px",
      nzContent:PermissionCard,
      nzComponentParams:{
        file:this.rootFile
      },
      nzClosable: false,
      nzOnOk: (page) => new Promise(resolve =>{
        this.settings.defaultPermission=page.permission
        console.log(this.settings)
        let data={
          ids:[this.rootFile?.id||''],
          permission:this.rootFile?.permission
        }
        BBYUtils.handelResult(
          this.diskService.editPermission(data),
          res => {
            this.message.success('根目录权限已修改！')
          },
          res => {
            this.message.error(res.msg)
          }
        )
        resolve()
      })
    });
  }
}
